package org.stone.project.tx.handler.msgSendUserTypeHandler;

import cn.hutool.core.util.ObjectUtil;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.stone.project.tx.config.CtxHolderProxy;
import org.stone.project.tx.handle.MsgSendUserTypeHandler;
import org.stone.project.tx.model.Message;
import org.stone.project.tx.pojo.dto.UuidUserDto;
import org.stone.project.tx.prop.TxProp;
import org.stone.project.tx.sysEnum.MsgType;
import org.stone.project.tx.sysEnum.ReceiveType;
import org.stone.project.tx.utils.ResultUtil;

import java.util.List;
import java.util.Map;

/**
 * 角色发送
 */
@Service
public class UserMsgSendAllTypeHandler implements MsgSendUserTypeHandler {
    @Autowired
    private TxProp txProp;

    @Override
    public ReceiveType getReceiveType() {
        return ReceiveType.ALL;
    }

    @Override
    public void sendMsg(Message msg) {
        Map<ChannelHandlerContext, UuidUserDto> userCtx = CtxHolderProxy.getAllCtx();
        userCtx.forEach((key,value)->{
            MsgType msgType = MsgType.getInstance(msg.getContentType());
            /**
             * 只有在线或消息类别真确才会被发送
             */
            if (ObjectUtil.isNotNull(key)  && ObjectUtil.isNotNull(msgType)){
                key.writeAndFlush(ResultUtil.ok(msgType,msg.getContent()));
            }
        });
    }
}
